/**
 * Copyright (c) 2018, dreamkaylee@foxmail.com All Rights Reserved.
 */

package com.common.util;

import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

/**
 * 项目参数工具类
 * @author limk
 * @date 2018年2月12日 下午6:01:58
 * @version 1.0
 */
public class ResourceUtil {

	private static final ResourceBundle BUNDLE = java.util.ResourceBundle.getBundle("config/sys");

	/**
	 * 获取项目路径
	 * @param request
	 * @return
	 */
	public static String getPorjectPath(HttpServletRequest request) {
		String porjectPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getLocalPort() + request.getContextPath();
		return porjectPath;
	}
	
	/**
	 * 获得请求路径
	 * @param request
	 * @return
	 */
	public static String getRequestPath(HttpServletRequest request) {
		String and = "&";
		String requestPath = request.getRequestURI() + "?" + request.getQueryString();
		// 去掉其他参数
		if (requestPath.indexOf(and) > -1) {
			requestPath = requestPath.substring(0, requestPath.indexOf("&"));
		}
		// 去掉项目路径
		requestPath = requestPath.substring(request.getContextPath().length() + 1);
		return requestPath;
	}
	
	/**
	 * 获取项目根目录
	 * @return
	 */
	public static String getPorjectPath() {
		String nowpath; // 当前tomcat的bin目录的路径
		String tempdir;
		nowpath = System.getProperty("user.dir");
		// 把bin文件夹变到 webapps文件里面
		tempdir = nowpath.replace("bin", "webapps"); 
		// 拼成D:\java\software\apache-tomcat-6.0.14\webapps\sz_pro
		tempdir += "\\"; 
		return tempdir;
	}

	/**
	 * 获取项目静态文件根目录
	 * @return
	 */
	public static String getSysPath() {
		String path = Thread.currentThread().getContextClassLoader().getResource("").toString();
		String temp = path.replaceFirst("file:/", "").replaceFirst("WEB-INF/classes/", "");
		String separator = System.getProperty("file.separator");
		String resultPath = temp.replaceAll("/", separator + separator).replaceAll("%20", " ");
		return resultPath;
	}
	
	/**
	 * 获取项目Class根目录
	 * @return
	 */
	public static String getClassPath() {
		String path = Thread.currentThread().getContextClassLoader().getResource("").toString();
		String temp = path.replaceFirst("file:/", "");
		String separator = System.getProperty("file.separator");
		String resultPath = temp.replaceAll("/", separator + separator);
		return resultPath;
	}
	
	/**
	 * 获取配置文件参数
	 * @param name
	 * @return
	 */
	public static final String getConfigByName(String name) {
		return BUNDLE.getString(name);
	}

    /**
     * 获取项目名
     * @return
     */
    public static String getProjectName() {
        return BUNDLE.getString("projectName");
    }
    
    /**
     * 获取随机码的长度
     * @return
     */
    public static String getRandCodeLength() {
    	return BUNDLE.getString("randCodeLength");
    }

	public static void main(String[] args) {
		System.out.println(getPorjectPath());
		System.out.println(getSysPath());
		System.out.println(getClassPath());
	}
}
